import { Component, OnInit } from '@angular/core';
import { Farm } from "../../../entity/farm.model";
import { FarmService } from "../../../service/farm.service";
import { PowerLoss } from "../../../entity/power-loss.model";
import { ActualWindPowerService } from "../../../service/actual-wind-power.service";

@Component({
	selector: 'app-farm-power-assess',
	templateUrl: './farm-power-assess.component.html',
	styleUrls: ['./farm-power-assess.component.css']
})
export class FarmPowerAssessComponent implements OnInit {

	//时间选择
	public years: string[] = ["2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "2012", "2011", "2010"]
	public currentYear: string = new Date().getFullYear().toString();
	public months: string[] = ["12", "11", "10", "9", "8", "7", "6", "5", "4", "3", "2", "1"];
	public currentMonth: string = (new Date().getMonth()+1).toString();

	//当前风场
	public farms: Farm[] = [];
	public currentFarm: Farm;
	private changeFarm: Farm;
	private farmId: number;

	//风场损失电量数据集
	public powerLossData: PowerLoss[] = [];
	public originalPowerLoss: PowerLoss[] = [];

	//风机损失电量数据集
	public turbPowerLossData: PowerLoss[] = [];
	public turbOriginalPowerLoss: PowerLoss[] = [];

	//日期类型
	private dateType: number;

	//风场年度发电能力评估（好，中，差）
	public powerJudge: string;

	//风场发电损失原因
	public powerLossReason: string;
	//月份发电损失原因
	public powerLossReasonMonth: PowerLossReason[] = [];
	//风机发电损失原因
	public powerLossReasonTurb: PowerLossReason[] = [];

	//风场年度月份发电能力评估
	public monthsPowerCapacity: PowerLoss[] = [];
	public monthsPowerCapacityDad: PowerLoss[] = [];

	//风场年度风机发电能力评估
	public turbPowerCapacity: PowerLoss[] = [];
	public turbPowerCapacityDad: PowerLoss[] = [];

	//损失电量表格统计(年度)
	actualPowerTotal: number;
	limitLossTotal: number;
	performLossTotal: number;
	stopLossTotal: number;
	badLossTotal: number;
	usePercentTotal: number;

	///////////////////////////////////////////////////////////////////////////////////////////(月度)
	//风场损失电量数据集(月度)
	public powerLossMonthData: PowerLoss[] = [];
	public originalPowerLossMonth: PowerLoss[] = [];

	//风机损失电量数据集
	public turbPowerLossDataMonth: PowerLoss[] = [];
	public turbOriginalPowerLossMonth: PowerLoss[] = [];

	//损失电量表格统计(月度)
	monthActualPowerTotal: number;
	monthLimitLossTotal: number;
	monthPerformLossTotal: number;
	monthStopLossTotal: number;
	monthBadLossTotal: number;
	monthUsePercentTotal: number;

	//风场月度发电能力评估（好，中，差）
	public powerJudgeMonth: string;

	//风场月度发电损失原因
	public monthPowerLossReason: string;
	public powerLossReasonDate: PowerLossReason[] = [];
	public turbinePowerLossReasonMonth: PowerLossReason[] = [];

	//风场月度每日发电能力评估
	public datePowerCapacity: PowerLoss[] = [];
	public datePowerCapacityDad: PowerLoss[] = [];

	//风场月度风机发电能力评估
	public turbMonthPowerCapacity: PowerLoss[] = [];
	public turbMonthPowerCapacityDad: PowerLoss[] = [];

	/**
	 * 当前选中下标
	 * 
	 * @type {number}@memberof PowerCompareComponent
	 */
	active: number = 0;

	/**
	 * echart series(年,饼图)
	* 
	* @type {any[]}@memberof TurbineTrendComponent
	*/
	yearPieSeries = [];
	monthPieSeries = [];

	//风场每月发电能力评估,风场下风机发电能力评估
	yearFarmColumnSeries = [];
	turbineColumnSeries = [];

	//风场，风机柱状图X轴
	xAxisData: string[] = [];
	xAxisDataturb: string[] = [];

	//风场月度每日发电能力评估
	monthFarmColumnSeries = [];
	turbMonthFarmColumnSeries = [];
	xAxisDataMonth: string[] = [];
	xAxisDataTurbName: string[] = [];

	constructor(
		private farmService: FarmService,
		private actualWindPowerService: ActualWindPowerService,
	) { }

	ngOnInit() {
		this.farmService.getFarms()
			.then(res => {
				if (res) {
					this.farms = [...res];
					this.currentFarm = this.farms[0];
					this.changeFarm = this.farms[0];
					this.onSearch(this.active)
				}
			})
			.catch(err => {
				console.log(err)
			})
	}

	onFarmClick(farm: Farm) {
		this.currentFarm = farm;
		if (this.changeFarm.farmId != this.currentFarm.farmId) {
			this.powerJudge = '';
			this.powerLossReason = '';
			this.monthsPowerCapacity = [];
			this.monthsPowerCapacityDad = [];
			this.powerLossReasonMonth = [];
			this.turbPowerCapacity = [];
			this.turbPowerCapacityDad = [];
			this.powerLossReasonTurb = [];

			this.powerJudgeMonth = '';
			this.monthPowerLossReason = '';
			this.datePowerCapacity = [];
			this.datePowerCapacityDad = [];
			this.powerLossReasonDate = [];
			this.turbMonthPowerCapacity = [];
			this.turbMonthPowerCapacityDad = [];
			this.turbinePowerLossReasonMonth = [];
		}
		this.onSearch(this.active)
	}

	public onClickHeader(n: number) {
		this.active = n;
		this.onSearch(this.active)
	}

	public onChangeYear(event) {
		this.currentYear = event;
	}

	public onChangeMonth(event) {
		this.currentMonth = event;
	}

	public onSearch(active: number) {
		this.initEchartsData(active);
	}

	initEchartsData(active: number) {
		if (active == 0) {
			this.dateType = 3;
			this.farmId = this.currentFarm.farmId;

			Promise.all(
				[this.actualWindPowerService.getYearPowerLossDataByFarmId(this.farmId, this.dateType, this.currentYear),
				this.actualWindPowerService.getYearTurbinePowerLossDataByFarmId(this.farmId, this.dateType, this.currentYear)
				])
				.then(res => {
					this.powerLossData = [];
					this.originalPowerLoss = [];
					this.turbOriginalPowerLoss = [];
					this.turbPowerLossData = [];
					this.monthsPowerCapacity = [];
					this.turbPowerCapacity = [];
					this.turbPowerCapacityDad = [];
					this.monthsPowerCapacityDad = [];
					this.powerLossReasonTurb = [];
					this.powerLossReasonMonth = [];

					//风场损失电量数据
					this.originalPowerLoss = [...res[0]];
					this.powerLossData.push(...this.originalPowerLoss);

					//风机损失电量数据
					this.turbOriginalPowerLoss = [...res[1]];
					this.turbPowerLossData.push(...this.turbOriginalPowerLoss);

					//风场每月电量损失柱状图data
					let actualPowerArr: number[] = [];
					let limitLossArr: number[] = [];
					let performLossArr: number[] = [];
					let stopLossArr: number[] = [];
					let badLossArr: number[] = [];
					let usePercentArr: number[] = [];

					//风场风机电量损失柱状图data
					let monthActualPowerArr: number[] = [];
					let monthLimitLossArr: number[] = [];
					let monthPerformLossArr: number[] = [];
					let monthStopLossArr: number[] = [];
					let monthBadLossArr: number[] = [];
					let monthUsePercentArr: number[] = [];

					//损失总量表格数据统计
					this.actualPowerTotal = 0;
					this.limitLossTotal = 0;
					this.performLossTotal = 0;
					this.stopLossTotal = 0;
					this.badLossTotal = 0;

					//风机柱图X轴(风机名称)
					let xAxisDataturb: string[] = [];
					let xAxisData: string[] = [];

					this.powerLossData.forEach((o, index) => {
						if (o != null) {
							this.actualPowerTotal += o.actualPower;
							this.limitLossTotal += o.limitLoss;
							this.performLossTotal += o.performLoss;
							this.stopLossTotal += o.stopLoss;
							this.badLossTotal += o.badLoss;

							actualPowerArr[index] = (o.actualPower)
							limitLossArr[index] = (o.limitLoss)
							performLossArr[index] = (o.performLoss)
							stopLossArr[index] = (o.stopLoss)
							badLossArr[index] = (o.badLoss)
							usePercentArr[index] = (o.usePercent)

							if (o.usePercent >= 90) {
								this.monthsPowerCapacity.push(o);
							}
							if (o.usePercent < 60) {
								this.monthsPowerCapacityDad.push(o);
							};
						};
					});

					//影响风场年度发电能力的损耗原因(饼图统计)
					this.powerLossReason = this.getReasonPowerLoss(this.stopLossTotal, this.limitLossTotal, this.performLossTotal, this.badLossTotal);

					//影响风场月每月发电能力的损耗原因（月度柱状图统计）
					this.monthsPowerCapacityDad.forEach(o => {
						if (o != null) {
							let powerLossJudjeMonth = []
							powerLossJudjeMonth.push(
								{ name: "停机损失", value: o.stopLoss },
								{ name: "限电损失", value: o.limitLoss },
								{ name: "性能损失", value: o.performLoss },
								{ name: "坏点损失", value: o.badLoss },
							)
							let maxLossMonth = this.powerLossJudje(o.stopLoss, o.limitLoss, o.performLoss, o.badLoss);
							powerLossJudjeMonth.forEach(k => {
								if (k.value == maxLossMonth) {
									let tuebPowerLossReason = {
										name: o.date,
										lossReason: k.name
									}
									this.powerLossReasonMonth.push(tuebPowerLossReason);
								}
							})
						}
					})

					//表格能量利用率统计
					this.usePercentTotal = ((this.actualPowerTotal) / (this.actualPowerTotal + this.limitLossTotal + this.performLossTotal + this.stopLossTotal)) * 100;
					this.usePercentTotal = this.usePercentTotal ? this.usePercentTotal : 0;

					//发电能力评估
					if (this.usePercentTotal > 0) {
						if (this.usePercentTotal >= 90) {
							this.powerJudge = "好"
						}
						if (this.usePercentTotal < 80) {
							this.powerJudge = "差"
						}
					}

					this.turbPowerLossData.forEach(o => {
						if (o != null) {
							monthActualPowerArr.push(o.actualPower)
							monthLimitLossArr.push(o.limitLoss)
							monthPerformLossArr.push(o.performLoss)
							monthStopLossArr.push(o.stopLoss)
							monthBadLossArr.push(o.badLoss)
							monthUsePercentArr.push(o.usePercent)
							xAxisDataturb.push(o.deviceName)

							if (o.usePercent >= 90) {
								this.turbPowerCapacity.push(o)
							}
							if (o.usePercent < 60) {
								this.turbPowerCapacityDad.push(o)
							}
						}
					})

					//影响风机发电能力损耗原因（风机柱状图统计）
					this.turbPowerCapacityDad.forEach(o => {
						let powerLossJudjeturb = [];
						powerLossJudjeturb.push(
							{ name: "停机损失", value: o.stopLoss },
							{ name: "限电损失", value: o.limitLoss },
							{ name: "性能损失", value: o.performLoss },
							{ name: "坏点损失", value: o.performLoss },
						)
						let maxLossturb = this.powerLossJudje(o.stopLoss, o.limitLoss, o.performLoss, o.badLoss);
						powerLossJudjeturb.forEach(k => {
							if (k.value == maxLossturb) {
								let tuebPowerLossReason = {
									name: o.deviceName,
									lossReason: k.name
								}
								this.powerLossReasonTurb.push(tuebPowerLossReason)
							}
						})
					})

					//风场柱状图数据对象
					let columnSeries = {
						actualPower: actualPowerArr,
						limitLoss: limitLossArr,
						performLoss: performLossArr,
						stopLoss: stopLossArr,
						badLoss: badLossArr,
						usePercent: usePercentArr
					}

					//风机柱状图数据对象
					let monthColumnSeries = {
						actualPower: monthActualPowerArr,
						limitLoss: monthLimitLossArr,
						performLoss: monthPerformLossArr,
						stopLoss: monthStopLossArr,
						badLoss: monthBadLossArr,
						usePercent: monthUsePercentArr
					}

					this.xAxisData = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'];
					this.xAxisDataturb = xAxisDataturb;

					this.yearFarmColumnSeries = [
						{
							name: '实发电量',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeries.actualPower //[232.06, 163.09, 242.91, 302.84, 389.45, 287.05, 261.41, 199.73, 145.57, 195.77]
						},
						{
							name: '性能损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeries.performLoss //[5.28, 3.96, 8.24, 15.26, 10.60, 14.07, 16.23, 18.11, 7.13, -3.20]
						},
						{
							name: '停机损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeries.stopLoss //[24.84, 31.21, 13.21, 5.72, 3.93, 5.65, 6.70, 12.74, 11.10, 7.40]
						},

						{
							name: '限电损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeries.limitLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '坏点损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeries.badLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '能量利用率',
							type: 'line',
							yAxisIndex: 1,
							data: columnSeries.usePercent //[87.9599, 78.1057, 86.8927, 93.142, 96.403, 93.5712, 91.9388, 86.6201, 88.8768, 97.8957] 
						},
					]

					this.yearPieSeries = [
						{
							name: '电量统计',
							type: 'pie',
							radius: '45%',
							center: ['50%', '50%'],
							data: [
								{ value: this.actualPowerTotal, name: '实发电量' },
								{ value: this.performLossTotal, name: '性能损失' },
								{ value: this.stopLossTotal, name: '停机损失' },
								{ value: this.limitLossTotal, name: '限电损失' },
								{ value: this.badLossTotal, name: '坏点损失' },
							],
							label: {
								normal: {
									formatter: "{b}({d}%)"
								}
							},
							labelLine: {
								normal: {
									length: 6,
									length2: 6
								}
							},
							color: ['#22b14c', '#a349a4', '#ff7f27', '#00a2e8', '#FF0000'],
							itemStyle: {
								emphasis: {
									shadowBlur: 10,
									shadowOffsetX: 0,
									shadowColor: 'rgba(0, 0, 0, 0.5)'
								}
							}
						}
					]
					this.turbineColumnSeries = [
						{
							name: '实发电量',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: monthColumnSeries.actualPower//[232.06, 163.09, 242.91, 302.84, 389.45, 287.05, 261.41, 199.73, 145.57, 195.77]
						},
						{
							name: '性能损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: monthColumnSeries.performLoss//[5.28, 3.96, 8.24, 15.26, 10.60, 14.07, 16.23, 18.11, 7.13, -3.20]
						},
						{
							name: '停机损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: monthColumnSeries.stopLoss//[24.84, 31.21, 13.21, 5.72, 3.93, 5.65, 6.70, 12.74, 11.10, 7.40]
						},

						{
							name: '限电损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: monthColumnSeries.limitLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '坏点损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: monthColumnSeries.badLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '能量利用率',
							type: 'line',
							yAxisIndex: 1,
							data: monthColumnSeries.usePercent  //[87.9599, 78.1057, 86.8927, 93.142, 96.403, 93.5712, 91.9388, 86.6201, 88.8768, 97.8957] 
						},
					]
				})
				.catch(error => {
					console.log("app-farm-power-assess", error);
				});

		} else {
			let time = new Date(this.currentYear + '-' + this.currentMonth);
			this.dateType = 2;
			this.farmId = this.currentFarm.farmId;

			Promise.all(
				[this.actualWindPowerService.getMonthPowerLossDataByFarmId(this.farmId, this.dateType, time),
				this.actualWindPowerService.getMonthTurbinePowerLossDataByFarmId(this.farmId, this.dateType, time)
				])
				.then(res => {
					console.log(res)
					this.originalPowerLossMonth = [];
					this.powerLossMonthData = [];
					this.turbOriginalPowerLossMonth = [];
					this.turbPowerLossDataMonth = [];
					this.datePowerCapacityDad = [];
					this.turbMonthPowerCapacityDad = [];
					this.datePowerCapacity = [];
					this.turbMonthPowerCapacity = [];
					this.powerLossReasonDate = [];
					this.turbinePowerLossReasonMonth = [];

					this.originalPowerLossMonth = [...res[0]];
					this.powerLossMonthData = [...this.originalPowerLossMonth];

					//月度风机损失电量数据
					this.turbOriginalPowerLossMonth = [...res[1]];
					this.turbPowerLossDataMonth.push(...this.turbOriginalPowerLossMonth);

					//风场当月每日电量损失柱状图data
					let actualPowerArr: number[] = [];
					let limitLossArr: number[] = [];
					let performLossArr: number[] = [];
					let stopLossArr: number[] = [];
					let badLossArr: number[] = [];
					let usePercentArr: number[] = [];

					//风场风机当月电量损失柱状图data
					let turbActualPowerArr: number[] = [];
					let turbLimitLossArr: number[] = [];
					let turbPerformLossArr: number[] = [];
					let turbStopLossArr: number[] = [];
					let turbBadLossArr: number[] = [];
					let turbUsePercentArr: number[] = [];

					//损失总量表格数据统计
					this.monthActualPowerTotal = 0;
					this.monthLimitLossTotal = 0;
					this.monthPerformLossTotal = 0;
					this.monthStopLossTotal = 0;
					this.monthBadLossTotal = 0;

					let xAxisDataMonth = [];
					let xAxisDataTurbName = [];

					this.powerLossMonthData.forEach(o => {
						if (o != null) {
							this.monthActualPowerTotal += o.actualPower;
							this.monthLimitLossTotal += o.limitLoss;
							this.monthPerformLossTotal += o.performLoss;
							this.monthStopLossTotal += o.stopLoss;
							this.monthBadLossTotal += o.badLoss;

							actualPowerArr.push(o.actualPower)
							limitLossArr.push(o.limitLoss)
							performLossArr.push(o.performLoss)
							stopLossArr.push(o.stopLoss)
							badLossArr.push(o.badLoss)
							usePercentArr.push(o.usePercent)

							xAxisDataMonth.push(o.date.substr(o.date.length - 2))

							if (o.usePercent >= 90) {
								this.datePowerCapacity.push(o)
							}
							if (o.usePercent < 60) {
								this.datePowerCapacityDad.push(o);
							}
						}
					})


					this.turbPowerLossDataMonth.forEach(o => {
						if (o != null) {
							turbActualPowerArr.push(o.actualPower)
							turbLimitLossArr.push(o.limitLoss)
							turbPerformLossArr.push(o.performLoss)
							turbStopLossArr.push(o.stopLoss)
							turbBadLossArr.push(o.badLoss)
							turbUsePercentArr.push(o.usePercent)

							xAxisDataTurbName.push(o.deviceName)

							if (o.usePercent >= 90) {
								this.turbMonthPowerCapacity.push(o)
							}
							if (o.usePercent < 60) {
								this.turbMonthPowerCapacityDad.push(o)
							}
						}
					})

					//表格能量利用率统计
					this.monthUsePercentTotal = ((this.monthActualPowerTotal) / (this.monthActualPowerTotal + this.monthLimitLossTotal + this.monthPerformLossTotal + this.monthStopLossTotal + this.monthBadLossTotal)) * 100;
					this.monthUsePercentTotal = this.monthUsePercentTotal ? this.monthUsePercentTotal : 0;

					//发电能力评估
					if (this.monthUsePercentTotal > 0) {
						if (this.monthUsePercentTotal >= 90) {
							this.powerJudgeMonth = "好"
						}
						if (this.monthUsePercentTotal < 80) {
							this.powerJudgeMonth = "差"
						}
					}

					//影响风场月度每日发电能力的损耗原因
					let powerLossReasonCurtDay: PowerLossReason[] = [];
					powerLossReasonCurtDay = this.getReasonPowerLossTurb(this.datePowerCapacityDad);
					this.powerLossReasonDate = [...powerLossReasonCurtDay];

					// this.datePowerCapacityDad.forEach(o => {
					// 	let powerLossJudjeMonth = []
					// 	powerLossJudjeMonth.push(
					// 		{ name: "停机损失", value: o.stopLoss },
					// 		{ name: "限电损失", value: o.limitLoss },
					// 		{ name: "性能损失", value: o.performLoss },
					// 		{ name: "坏点损失", value: o.badLoss },
					// 	)
					// 	let maxLossMonth = this.powerLossJudje(o.stopLoss, o.limitLoss, o.performLoss, o.badLoss);
					// 	powerLossJudjeMonth.forEach(k => {
					// 		if (k.value == maxLossMonth) {
					// 			let tuebPowerLossReason = {
					// 				name: o.date,
					// 				lossReason: k.name
					// 			}
					// 			this.powerLossReasonDate.push(tuebPowerLossReason)
					// 		}
					// 	})
					// })

					//影响风场月度发电能力的损耗原因
					this.monthPowerLossReason = this.getReasonPowerLoss(this.monthStopLossTotal, this.monthLimitLossTotal, this.monthPerformLossTotal, this.monthBadLossTotal)

					//风场月度柱状图数据对象、X轴
					this.xAxisDataMonth = xAxisDataMonth
					let columnSeriesMonth = {
						actualPower: actualPowerArr,
						limitLoss: limitLossArr,
						performLoss: performLossArr,
						stopLoss: stopLossArr,
						badLoss: badLossArr,
						usePercent: usePercentArr
					}

					//风场月度风机柱状图数据对象、X轴
					this.xAxisDataTurbName = xAxisDataTurbName
					let turbineFarmColumnSeries = {
						actualPower: turbActualPowerArr,
						limitLoss: turbLimitLossArr,
						performLoss: turbPerformLossArr,
						stopLoss: turbStopLossArr,
						badLoss: turbBadLossArr,
						usePercent: turbUsePercentArr
					}

					//影响风机发电能力损耗原因
					let powerLossReasonCurtTurb: PowerLossReason[] = [];
					powerLossReasonCurtTurb = this.getReasonPowerLossTurb(this.turbMonthPowerCapacityDad);
					this.turbinePowerLossReasonMonth = [...powerLossReasonCurtTurb];

					// this.turbMonthPowerCapacityDad.forEach(o => {
					// 	let powerLossJudjeMonthturb = []
					// 	powerLossJudjeMonthturb.push(
					// 		{ name: "停机损失", value: o.stopLoss },
					// 		{ name: "限电损失", value: o.limitLoss },
					// 		{ name: "性能损失", value: o.performLoss },
					// 		{ name: "坏点损失", value: o.badLoss },
					// 	)
					// 	let maxLossturb = this.powerLossJudje(o.stopLoss, o.limitLoss, o.performLoss, o.badLoss);
					// 	powerLossJudjeMonthturb.forEach(k => {
					// 		if (k.value == maxLossturb) {
					// 			let tuebPowerLossReason = {
					// 				name: o.turbineName,
					// 				lossReason: k.name
					// 			}
					// 			this.turbinePowerLossReasonMonth.push(tuebPowerLossReason)
					// 		}
					// 	})
					// })

					this.monthFarmColumnSeries = [
						{
							name: '实发电量',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeriesMonth.actualPower//[232.06, 163.09, 242.91, 302.84, 389.45, 287.05, 261.41, 199.73, 145.57, 195.77]
						},
						{
							name: '性能损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeriesMonth.performLoss//[5.28, 3.96, 8.24, 15.26, 10.60, 14.07, 16.23, 18.11, 7.13, -3.20]
						},
						{
							name: '停机损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeriesMonth.stopLoss//[24.84, 31.21, 13.21, 5.72, 3.93, 5.65, 6.70, 12.74, 11.10, 7.40]
						},
						{
							name: '限电损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeriesMonth.limitLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '坏点损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: columnSeriesMonth.badLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '能量利用率',
							type: 'line',
							yAxisIndex: 1,
							data: columnSeriesMonth.usePercent //[87.9599, 78.1057, 86.8927, 93.142, 96.403, 93.5712, 91.9388, 86.6201, 88.8768, 97.8957] 
						},
					]

					this.monthPieSeries = [
						{
							type: 'pie',
							radius: '45%',
							center: ['50%', '50%'],
							data: [
								{ value: this.monthActualPowerTotal, name: '实发电量' },
								{ value: this.monthPerformLossTotal, name: '性能损失' },
								{ value: this.monthStopLossTotal, name: '停机损失' },
								{ value: this.monthLimitLossTotal, name: '限电损失' },
								{ value: this.monthBadLossTotal, name: '坏点损失' }
							],
							label: {
								normal: {
									formatter: "{b}({d}%)"
								}
							},
							labelLine: {
								normal: {
									length: 6,
									length2: 6
								}
							},
							color: ['#22b14c', '#a349a4', '#ff7f27', '#00a2e8', '#FF0000'],
							itemStyle: {
								emphasis: {
									shadowBlur: 10,
									shadowOffsetX: 0,
									shadowColor: 'rgba(0, 0, 0, 0.5)'
								}
							}
						}
					]

					this.turbMonthFarmColumnSeries = [
						{
							name: '实发电量',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: turbineFarmColumnSeries.actualPower//[232.06, 163.09, 242.91, 302.84, 389.45, 287.05, 261.41, 199.73, 145.57, 195.77]
						},
						{
							name: '性能损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: turbineFarmColumnSeries.performLoss//[5.28, 3.96, 8.24, 15.26, 10.60, 14.07, 16.23, 18.11, 7.13, -3.20]
						},
						{
							name: '停机损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: turbineFarmColumnSeries.stopLoss//[24.84, 31.21, 13.21, 5.72, 3.93, 5.65, 6.70, 12.74, 11.10, 7.40]
						},

						{
							name: '限电损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: turbineFarmColumnSeries.limitLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '坏点损失',
							type: 'bar',
							stack: '总量',
							label: {
								normal: {
									position: 'insideRight'
								}
							},
							data: turbineFarmColumnSeries.badLoss//[1.65, 10.54, 15.19, 1.31, 0, 0, 0, 0, 0, 0]
						},
						{
							name: '能量利用率',
							type: 'line',
							yAxisIndex: 1,
							data: turbineFarmColumnSeries.usePercent  //[87.9599, 78.1057, 86.8927, 93.142, 96.403, 93.5712, 91.9388, 86.6201, 88.8768, 97.8957] 
						},
					]

				})
				.catch(error => {
					console.log("app-farm-power-assess", error);
				});
		}
	}

	//风场年度损耗原因函数
	private getReasonPowerLoss(stopLossTotal: number, limitLossTotal: number, performLossTotal: number, badLossTotal: number) {

		//影响风场发电能力的损耗原因
		let powerLossJudjeArr: any = []
		let powerLossReason: string;
		powerLossJudjeArr.push(
			{ name: "停机损失", value: stopLossTotal },
			{ name: "限电损失", value: limitLossTotal },
			{ name: "性能损失", value: performLossTotal },
			{ name: "坏点损失", value: badLossTotal },
		)
		//损失的量最大的值为损失原因
		let maxLoss = this.powerLossJudje(stopLossTotal, limitLossTotal, performLossTotal, badLossTotal);
		if (maxLoss > 0) {
			powerLossJudjeArr.forEach(o => {
				if (o.value == maxLoss) {
					powerLossReason = o.name;
				}
			})
		} else {
			powerLossReason = '';
		}

		return powerLossReason
	}

	//风场每月、风场风机损耗原因函数
	private getReasonPowerLossTurb(powerCapacityDad: PowerLoss[]) {
		let powerLossReasonDate: PowerLossReason[] = [];
		let powerLossJudjeMonth = [];
	
		powerCapacityDad.forEach(o => {
			powerLossJudjeMonth.push(
				{ name: "停机损失", value: o.stopLoss },
				{ name: "限电损失", value: o.limitLoss },
				{ name: "性能损失", value: o.performLoss },
				{ name: "坏点损失", value: o.badLoss },
			)
			let maxLossMonth = this.powerLossJudje(o.stopLoss, o.limitLoss, o.performLoss, o.badLoss);
			powerLossJudjeMonth.forEach(k => {
				if (k.value == maxLossMonth) {
					let turbPowerLossReason = {
						name: o.date,
						lossReason: k.name
					}
					powerLossReasonDate.push(turbPowerLossReason)
				}
			})
		})

		return powerLossReasonDate;

	}

	private powerLossJudje(a: number, b: number, c: number, d: number) {
		// var max =  c - (a - b > 0 ? a : b) > 0 ? c : a - b > 0 ? a : b;
		var max = Math.max(a, b, c, d);
		return max
	}

}

interface seriesDataArr {
	actualPower: any[];
	limitLoss: any[];
	performLoss: any[];
	StopLoss: any[];
	usePercent: any[];
}

interface PowerLossReason {
	name: string;
	lossReason: string;
}



// WEBPACK FOOTER //
// ./src/app/module/performance/farm-power-assess/farm-power-assess.component.ts